﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for CarOffpeakDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarOffpeakDB
    {
        public CarOffpeakDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarOffpeak CarOffpeak)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarOffpeakInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCarOffpeakId", CarOffpeak.ProviderCarOffpeakId);
            _sPara.Add("@ProviderCompanyId", CarOffpeak.ProviderCompanyId);
            _sPara.Add("@Description", CarOffpeak.Description);
            _sPara.Add("@IsOffpeak", CarOffpeak.IsOffpeak);
            _sPara.Add("@Rate", CarOffpeak.Rate);
            _sPara.Add("@RefCEUserId", CarOffpeak.RefCEUserId);
            _sPara.Add("@Status", CarOffpeak.Status);
            _sPara.Add("@Finfo1", CarOffpeak.FInfo1);
            _sPara.Add("@Dinfo1", CarOffpeak.DInfo1);

            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public CarOffpeak GetProviderCarOffpeak(Int64 ProviderCarOffpeakId)
        {
            CarOffpeak _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarOffpeakSelectSingleItem]";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCarOffpeakId", ProviderCarOffpeakId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarOffpeak();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        public CarOffpeak GetProviderCarOffpeak(Int64 ProviderCompanyId, bool isOffpeak)
        {
            CarOffpeak _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarOffpeakSelectSingleItemSelected]";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@IsOffpeak", isOffpeak);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarOffpeak();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        private CarOffpeak FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarOffpeak myCarOffpeak = new CarOffpeak();
            myCarOffpeak.ProviderCarOffpeakId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCarOffpeakId"].ToString());
            myCarOffpeak.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCarOffpeak.Description = myDataTable.Rows[RowId]["Description"].ToString();
            myCarOffpeak.IsOffpeak = Convert.ToBoolean(myDataTable.Rows[RowId]["IsOffpeak"].ToString());            
            myCarOffpeak.Rate = Convert.ToSingle(myDataTable.Rows[RowId]["Rate"].ToString());
            myCarOffpeak.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCarOffpeak.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            myCarOffpeak.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());
            if (myDataTable.Rows[RowId]["FInfo1"].ToString() != "")
                myCarOffpeak.FInfo1 = Convert.ToSingle(myDataTable.Rows[RowId]["FInfo1"].ToString());

            if (myDataTable.Rows[RowId]["DInfo1"].ToString() != "")
                myCarOffpeak.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["DInfo1"].ToString());
            else
                myCarOffpeak.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());
            return myCarOffpeak;
        }
    }
}
